//
//  ZZPRequest.m
//  DrivingWizard
//
//  Created by apple on 16/10/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ZZPRequest.h"
#import "AppDelegate.h"
/**
 *  存放 网络请求的线程
 */
static NSMutableArray *sg_requestTasks;
@implementation ZZPRequest
static  ZZPRequest* webUtil = nil;

+ (ZZPRequest *) getWebUtil
{
    @synchronized([ZZPRequest class])
    {
        if (!webUtil) {
            webUtil = [[[self class] alloc] init];
        }
        return webUtil;
    }
    return nil;
}

+ (void)netWorkStatus:(transferNetState)transfer {
    /**
     *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
     *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
     */
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        transfer(status);
    }];
}


+ (void)currentNetWorkStatus:(transNetState)transfer {
    /**
     *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
     *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
     */
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable) {
            transfer(NO);
        }else{
            transfer(YES);
        }
    }];
}




#pragma mark - AFnetworking manager getter

- (AFHTTPSessionManager *)createAFHTTPSessionManager
{
    
    static AFHTTPSessionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        //设置请求参数的类型:HTTP (AFJSONRequestSerializer,AFHTTPRequestSerializer)
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        //设置请求的超时时间
        manager.requestSerializer.timeoutInterval = 30.f;
        //设置服务器返回结果的类型:JSON (AFJSONResponseSerializer,AFHTTPResponseSerializer)
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",@"application/force-download",@"image/jpeg",@"application/octet-stream", nil];

    });
    return manager;
}



- (AFHTTPSessionManager*)sharedJsonHttpSession {
    static AFHTTPSessionManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [AFHTTPSessionManager manager];
        //声明上传的参数为json 格式
        sharedInstance.requestSerializer = [AFJSONRequestSerializer serializer];
        // 上传普通格式
        //sharedInstance.requestSerializer = [AFHTTPRequestSerializer serializer];
        //接收格式 指令为普通格式
        sharedInstance.responseSerializer = [AFHTTPResponseSerializer serializer];
        //manager.responseSerializer = [AFJSONResponseSerializer serializer];
        sharedInstance.requestSerializer.timeoutInterval = 30.f;
        sharedInstance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",@"application/force-download",@"application/octet-stream",@"image/jpeg", nil];
    });
    return sharedInstance;
}


-(void)postJsonNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    if (show) {
        [self addAnimation];
    }
//    NSMutableDictionary *tempParamsDic = [NSMutableDictionary dictionary];
   
//    if([params isKindOfClass:[NSDictionary class]] || params == nil){
//        tempParamsDic = [NSMutableDictionary dictionaryWithDictionary:params];
//        if ([userToken length] > 0) {
//            [tempParamsDic setValue:[NSString stringWithFormat:@"%@",userToken] forKey:@"Admin-Token"];
//        }
//        if ([[NSString stringWithFormat:@"%@",userInfoId] length] > 0) {
//            [tempParamsDic setValue:userInfoId forKey:@"userId"];
//        }
//        params = tempParamsDic;
//    }
    
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"%@",userToken] forKey:@"Admin-Token"];
    }

    NSLog(@"address:%@%@    %@",ADDRESSIP,urlString,params);

    AFHTTPSessionManager *manager = [self sharedJsonHttpSession];

    [manager POST:[NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString] parameters:params headers:tokenDic1 progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
        
        if([urlString containsString:@"/oss/downloadFromCrm?fileName"]){
            transfer(responseObject,nil);
            return;
        }

        responseObject = [responseObject mj_JSONObject];
        if([GetHandleString(responseObject, @"code") intValue] == 0 || [GetHandleString(responseObject, @"code") intValue] == 200){
            transfer(responseObject,nil);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }else{
            
            if([GetHandleString(responseObject, @"code") intValue] == 302){
                [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
                [ZZPUtils removeUserDefultsDataForkey:@"adminToken"];
                LoginViewController *login = [[LoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                [AppDelegate shareInstance].window.rootViewController = nav;
                return;
            }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                [self showEquipment:GetHandleString(responseObject, @"msg")];
                return;
            }
            
            [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
            NSError *error = [[NSError alloc] init];
            transfer(nil,error);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];

}



-(void)putJsonNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    if (show) {
        [self addAnimation];
    }
    NSMutableDictionary *tempParamsDic = [NSMutableDictionary dictionary];
   
    if([params isKindOfClass:[NSDictionary class]] || params == nil){
        tempParamsDic = [NSMutableDictionary dictionaryWithDictionary:params];
        if ([userToken length] > 0) {
            [tempParamsDic setValue:[NSString stringWithFormat:@"Bearer %@",userToken] forKey:@"Authorization"];
        }
        if ([[NSString stringWithFormat:@"%@",userInfoId] length] > 0) {
            if([GetHandleString(tempParamsDic, @"id") length] == 0){
                [tempParamsDic setValue:userInfoId forKey:@"id"];
            }
        }
        params = tempParamsDic;
    }
    
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"Bearer %@",userToken] forKey:@"Authorization"];
    }

    NSLog(@"address:%@%@    %@",ADDRESSIP,urlString,tempParamsDic);

    AFHTTPSessionManager *manager = [self sharedJsonHttpSession];

    [manager PUT:[NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString] parameters:params headers:tokenDic1 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
        responseObject = [responseObject mj_JSONObject];
        if([GetHandleString(responseObject, @"code") intValue] == 200){
            transfer(responseObject,nil);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }else{
            
            if([GetHandleString(responseObject, @"code") intValue] == 401){
                
                [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
            
                LoginViewController *login = [[LoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                NSLog(@"当前视图控制器：%@",[ZZPUtils activityViewController]);
                if([[ZZPUtils activityViewController] isKindOfClass:[UINavigationController class]]){
                    UINavigationController *na = (UINavigationController *)[ZZPUtils activityViewController];
                    if([[na.viewControllers firstObject] isKindOfClass:[LoginViewController class]]){
                        return;
                    }
                    [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
                    [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
                }else{
                    [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
                    [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
                }
                return;
            }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                [self showEquipment:GetHandleString(responseObject, @"msg")];
                return;
            }
            
            [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
            NSError *error = [[NSError alloc] init];
            transfer(nil,error);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];

}
-(void)deleteJsonNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    if (show) {
        [self addAnimation];
    }
    NSMutableDictionary *tempParamsDic = [NSMutableDictionary dictionary];
   
    if([params isKindOfClass:[NSDictionary class]] || params == nil){
        tempParamsDic = [NSMutableDictionary dictionaryWithDictionary:params];
        if ([userToken length] > 0) {
            [tempParamsDic setValue:[NSString stringWithFormat:@"Bearer %@",userToken] forKey:@"Authorization"];
        }
        if ([[NSString stringWithFormat:@"%@",userInfoId] length] > 0) {
            [tempParamsDic setValue:userInfoId forKey:@"id"];
        }
        params = tempParamsDic;
    }
    
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"Bearer %@",userToken] forKey:@"Authorization"];
    }

    NSLog(@"address:%@%@    %@",ADDRESSIP,urlString,tempParamsDic);

    AFHTTPSessionManager *manager = [self sharedJsonHttpSession];
    
    [manager DELETE:[NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString] parameters:params headers:tokenDic1 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
        responseObject = [responseObject mj_JSONObject];
        if([GetHandleString(responseObject, @"code") intValue] == 200){
            transfer(responseObject,nil);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }else{
            
            if([GetHandleString(responseObject, @"code") intValue] == 401){
                [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
                LoginViewController *login = [[LoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                NSLog(@"当前视图控制器：%@",[ZZPUtils activityViewController]);
                if([[ZZPUtils activityViewController] isKindOfClass:[UINavigationController class]]){
                    UINavigationController *na = (UINavigationController *)[ZZPUtils activityViewController];
                    if([[na.viewControllers firstObject] isKindOfClass:[LoginViewController class]]){
                        return;
                    }
                    [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
                    [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
                }else{
                    [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
                    [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
                }
                return;
            }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                [self showEquipment:GetHandleString(responseObject, @"msg")];
                return;
            }
            
            [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
            NSError *error = [[NSError alloc] init];
            transfer(nil,error);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];

}




/**
 *JSON方式获取数据 POST
 *urlStr:获取数据的url地址
 *
 */

-(void)postNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    if (show) {
        [self addAnimation];
    }
//    NSMutableDictionary *tempParamsDic = [NSMutableDictionary dictionary];
//   
//    if([params isKindOfClass:[NSDictionary class]] || params == nil){
//        tempParamsDic = [NSMutableDictionary dictionaryWithDictionary:params];
//        if ([userToken length] > 0) {
//            [tempParamsDic setValue:[NSString stringWithFormat:@"Bearer %@",userToken] forKey:@"Authorization"];
//        }
//        params = tempParamsDic;
//    }
    
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"%@",userToken] forKey:@"Admin-Token"];
    }

    NSLog(@"address:%@%@    %@",ADDRESSIP,urlString,params);

    AFHTTPSessionManager *manager = [self createAFHTTPSessionManager];

    [manager POST:[NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString] parameters:params headers:tokenDic1 progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
        
        if([urlString containsString:@"/oss/downloadFromCrm?fileName"]){
            transfer(responseObject,nil);
            return;
        }

        responseObject = [responseObject mj_JSONObject];
        if([GetHandleString(responseObject, @"code") intValue] == 0){
            transfer(responseObject,nil);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }else{
            
            if([GetHandleString(responseObject, @"code") intValue] == 302){
                [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
                [ZZPUtils removeUserDefultsDataForkey:@"adminToken"];
                LoginViewController *login = [[LoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                [AppDelegate shareInstance].window.rootViewController = nav;
                return;
            }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                [self showEquipment:GetHandleString(responseObject, @"msg")];
                return;
            }
            
            [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
            NSError *error = [[NSError alloc] init];
            transfer(nil,error);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];
}


-(void)getNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    if (show) {
        [self addAnimation];
    }
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"%@",userToken] forKey:@"Admin-Token"];
    }

    NSLog(@"address:%@%@    %@",ADDRESSIP,urlString,params);

    AFHTTPSessionManager *manager = [self sharedJsonHttpSession];
    
    NSString *qingQiuUrl = [NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString];
    qingQiuUrl = [qingQiuUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:qingQiuUrl parameters:params headers:tokenDic1 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
        if(![responseObject isKindOfClass:[NSString class]]){
            transfer(responseObject,nil);
        }else{
            responseObject = [responseObject mj_JSONObject];
            if([GetHandleString(responseObject, @"code") intValue] == 0){
                transfer(responseObject,nil);
                NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
            }else{
                
                if([GetHandleString(responseObject, @"code") intValue] == 302){
                    [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
                    [ZZPUtils removeUserDefultsDataForkey:@"adminToken"];
                    LoginViewController *login = [[LoginViewController alloc] init];
                    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                    [AppDelegate shareInstance].window.rootViewController = nav;
                    return;
                }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                    [self showEquipment:GetHandleString(responseObject, @"msg")];
                    return;
                }
                
                [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
                NSError *error = [[NSError alloc] init];
                transfer(nil,error);
                NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
            }
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];
}






-(void)uploadImageRequestURLString:(NSString *) urlString  andParas:(id) params andImageArray:(NSArray *)imageArray andShowHud:(BOOL)show andTransferGottenData:(transferValue) transfer {
    
    AFHTTPSessionManager *manager = [self createAFHTTPSessionManager];
    NSMutableDictionary *tokenDic1 = [NSMutableDictionary dictionary];
    if ([userToken length] > 0) {
        [tokenDic1 setValue:[NSString stringWithFormat:@"%@",userToken] forKey:@"Admin-Token"];
    }
    [manager POST:[NSString stringWithFormat:@"%@%@",ADDRESSIP,urlString] parameters:params headers:tokenDic1 constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSInteger i = 0; i < imageArray.count; i++) {
            NSDictionary *tempDic = imageArray[i];
            [formData appendPartWithFileData:[tempDic objectForKey:@"data"] name:@"file" fileName:GetHandleString(tempDic, @"name") mimeType:@"multipart/form-data"];
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenAnimation];
  
        if([GetHandleString(responseObject, @"code") intValue] == 0){
            transfer(responseObject,nil);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }else{
            
            if([GetHandleString(responseObject, @"code") intValue] == 302){
                [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
                [ZZPUtils removeUserDefultsDataForkey:@"adminToken"];
                LoginViewController *login = [[LoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
                [AppDelegate shareInstance].window.rootViewController = nav;
                return;
            }else if ([GetHandleString(responseObject, @"code") integerValue] == 9999){
                [self showEquipment:GetHandleString(responseObject, @"msg")];
                return;
            }
            
            [ZZPUtils showWarning:GetHandleString(responseObject, @"msg")];
            NSError *error = [[NSError alloc] init];
            transfer(nil,error);
            NSLog(@"请求地址:%@ 请求结果:%@",urlString,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenAnimation];
        NSLog(@"error:%@",error);
        if(error.code == -1009){
            [ZZPUtils showWarning:@"网络连接异常，请检查您的网络"];
            return;
        }
        NSDictionary *errDic = error.userInfo;
        [ZZPUtils showWarning:GetHandleString(errDic,@"NSLocalizedDescription")];
        transfer(nil,error);
    }];
}


//9999错误码代表为当前登录角色由普通用户改变为签约用户
- (void)showEquipment:(NSString *)string {
    SPAlertController *alert = [SPAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"\n%@",string] preferredStyle:SPAlertControllerStyleAlert];
    alert.messageFont = [UIFont systemFontOfSize:16];
    SPAlertAction *action = [SPAlertAction actionWithTitle:@"确定" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
        [ZZPUtils removeUserDefultsDataForkey:@"userInfo"];
        LoginViewController *login = [[LoginViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        NSLog(@"当前视图控制器：%@",[ZZPUtils activityViewController]);
        if([[ZZPUtils activityViewController] isKindOfClass:[UINavigationController class]]){
            UINavigationController *na = (UINavigationController *)[ZZPUtils activityViewController];
            if([[na.viewControllers firstObject] isKindOfClass:[LoginViewController class]]){
                return;
            }
            [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
           
        }else{
            [[ZZPUtils activityViewController] presentViewController:nav animated: YES completion:nil];
        }
    }];
    action.titleColor = mainColor;
    action.titleFont = [UIFont systemFontOfSize:15];
    alert.minDistanceToEdges = 30;
    [alert addAction:action];
    if (![[ZZPUtils activityViewController] isKindOfClass:[UIAlertController class]]) {
        [[ZZPUtils activityViewController] presentViewController:alert animated:YES completion:nil];
    }
}


//添加动画
- (void)addAnimation {
    if (self.HUD != nil) {
        _HUD.removeFromSuperViewOnHide = YES;
        [_HUD removeFromSuperview];
        _HUD.hidden = YES;
        _HUD = nil;
    }else {
        if([ZZPUtils activityViewController] != nil && [ZZPUtils activityViewController].view != nil){
            _HUD = [MBProgressHUD showHUDAddedTo:[ZZPUtils activityViewController].view animated:YES];
        }else{
            return;
        }
    }
    _HUD.mode = MBProgressHUDModeIndeterminate;
    _HUD.delegate = self;
    _HUD.backgroundColor = [UIColor clearColor];
}

- (void)hiddenAnimation {
    _HUD.removeFromSuperViewOnHide = YES;
    _HUD.hidden = YES;
    [_HUD removeFromSuperview];
    _HUD = nil;
    
}




- (NSMutableArray *)allTasks {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (sg_requestTasks == nil) {
            sg_requestTasks = [[NSMutableArray alloc] init];
        }
    });
    
    return sg_requestTasks;
}







@end
